#!/usr/bin/env python
# coding: utf-8

# ## 高德地图API
# 

# ### 地理编码
# >3.1 [地理编码](https://lbs.amap.com/api/webservice/guide/api/georegeo)
# >>3.1.1结构化地址含义：首先，地址肯定是一串字符，内含国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦等建筑物名称。按照由大区域名称到小区域名称组合在一起的字符。一个有效的地址应该是独一无二的。注意：针对大陆、港、澳地区的地理编码转换时可以将国家信息选择性的忽略，但省、市、城镇等级别的地址构成是不能忽略的。暂时不支持返回台湾省的详细地址信息。
# 
# >>3.1.2url:https://restapi.amap.com/v3/geocode/geo?parameters
# 
# >>3.2.3request.get
# 
# >>3.2.4参数：key,address

# In[7]:


#准备工作
import requests,json
import pandas as pd
zhang_key = "241cb34c3ba21b934c74a65177c504ec"


# In[8]:


# A-1
address = "广东省广州市中山大学南方学院"
geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {
    "key":zhang_key,
    "address":address
}
response = requests.get(url=geocode_url,params=params)


# In[9]:


response


# In[10]:


response.json()


# In[11]:


中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location


# ### 逆地理编码
# * 1.url:https://restapi.amap.com/v3/geocode/regeo?parameters

# In[12]:


def regeo(key,location,extensions="base",poitype=None,radius=None,roadlevel=None):
    """高德API逆地理编码获取"""
    regeo_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params_regeo ={
        "key":zhang_key,
        "location":location,
        "extensions":extensions,
        "poitype":poitype,
        "radius":radius,
        "roadlevel":roadlevel
    }
    r=requests.get(url=regeo_url,params=params_regeo)
    return r.json()


# In[13]:


regeo(key=zhang_key,location=中大南方_location,radius=3000)


# In[14]:


regeo_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params_regeo ={
    "key":zhang_key,
    "location":中大南方_location,
    "extensions":"all"
}
r=requests.get(url=regeo_url,params=params_regeo)


# In[15]:


r


# In[16]:


results=r.json()
results


# In[17]:


# 显示道路信息
pd.json_normalize(results['regeocode']['roads'])


# In[18]:


pd.json_normalize(results['regeocode']['pois'])


# In[ ]:





# ### 路径规划
# >3.2[路径规划](https://restapi.amap.com/v3/direction/walking?parameters)
# >>3.2.1产品介绍：路径规划API是一套以HTTP形式提供的步行、公交、驾车查询及行驶距离计算接口，返回JSON 或 XML格式的查询数据，用于实现路径规划功能的开发。由于道路/数据/算法的变更，很可能存在间隔一段时间后请求相同起终点的经纬度返回不同结果。
# 
# * 步行url:https://restapi.amap.com/v3/direction/walking?parameters
# 
#   公交url:https://restapi.amap.com/v3/direction/transit/integrated?parameters
#      
#   驾车url:https://restapi.amap.com/v3/direction/driving?parameters
# 
# >>3.2.3request.get
# 
# >>3.2.4参数：origin,destination

# In[19]:


# 准备起始点和终点
start_add = "广东省广州市中山大学南方学院"
stop_add = "广东省广州市龙岗"


# In[20]:


# 函数？！
def geocode(key,address,city=None):
    """高德API地理编码获取"""
    url ="https://restapi.amap.com/v3/direction/walking?parameters"
    params ={
        "key":key,
        "address":address
    }
    response = requests.get(url=geocode_url,params=params)
    results = response.json()['geocodes'][0]['location']
    return results


# In[21]:


results_起始点 = geocode(zhang_key,start_add)
results_起始点


# In[22]:


results_终点 = geocode(zhang_key,stop_add)
results_终点


# In[23]:


# 起点坐标
address = "广东省广州市中山大学南方学院"
geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {
    "key":zhang_key,
    "address":address
}
response = requests.get(url=geocode_url,params=params)


# In[24]:


# 终点坐标
address = "广东省广州市龙岗"
geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {
    "key":zhang_key,
    "address":address
}
response = requests.get(url=geocode_url,params=params)


# #### 步行

# In[25]:


walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
params_walking = {
    "key":zhang_key,
    "origin":results_起始点,
    "destination":results_终点
}
r = requests.get(url=walking_url,params=params_walking)


# In[26]:


r


# In[27]:


r.json()


# In[28]:


# 期中测试：计算距离？1、里面的数据是什么格式？str->int 2、是不是要会list循环遍历？3、运算？


# In[29]:


# 期中测试2：有多少个情况？每一个情况多少次？有多少个空？请转换换成字典作为结果？


# In[30]:


pd.json_normalize(r.json()['route']['paths'][0]['steps'])['distance'].to_list()


# #### 公交
# 1. URL:https://restapi.amap.com/v3/direction/transit/integrated?parameters
# 2. request.get()
# 3. key,origin,destination,city

# In[31]:


bus_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
bus_params = {
        "key":zhang_key,
        "origin":results_起始点,
        "destination":results_终点,
    }
response = requests.get(url=bus_url,params=bus_params)
results = response.json()


# In[32]:


r


# In[33]:


r.json()


# ### 搜索POI
# 1. 适用场景
# 2. 周边搜索
# 3. 多边形搜索
# 4. ID查询

# #### 关键字搜索
# 1. 通过用POI的关键字进行条件搜索，例如：肯德基、朝阳公园等；同时支持设置POI类型搜索，例如：银行

# In[34]:


def text(keywords,types,city=None,extensions='base',sig=None):
    url="https://restapi.amap.com/v3/place/text?parameters"
    params={
        "key":zhang_key,
        "keywords":keywords,
        "types":types,
        "city":city,
        "extensions":extensions,
        "sig":sig,
    }
    r=requests.get(url,params=params)
    results=r.json()
    return results


# In[35]:


results_关键字搜索=text('美食','050303','佛山')
results_关键字搜索


# #### 周边搜索
# 1. url:https://restapi.amap.com/v3/place/around?parameters 
# 2. request.get
# 3. location,keywords,types,radius,offset,page,extensions

# In[36]:


def around(location,keywords=None,types=None,radius=None,offset=None,page=None,extensions='base'):
    url="https://restapi.amap.com/v3/place/around?parameters"
    params={
        "key":zhang_key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "radius":radius,
        "offset":offset,
        "page":page,
        "extensions":extensions
    }
    r=requests.get(url,params=params)
    results=r.json()
    return results


# In[37]:


results_周边搜索=around(中大南方_location)
results_周边搜索


# ### 行政区域规划
# 1. 行政区域查询是一类简单的HTTP接口，根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息。
# 2. URL:https://restapi.amap.com/v3/config/district?parameters
# 3. request.get
# 4. keywords,subdistrict,extensions

# In[38]:


def district(keywords,subdistrict=None,page=None,extensions="base"):
    district_url = "https://restapi.amap.com/v3/config/district?parameters"
    district_params = {
        "key":zhang_key,
        "keywords":keywords,
        "subdistrict":subdistrict,
        "page":page,
        "extensions":extensions,
        "output":"json"
    }
    response = requests.get(district_url,district_params)
    results = response.json()
    return results


# In[39]:


results_行政查询 = district("广东")
results_行政查询


# In[ ]:





# ### IP定位
# 1. IP定位是一个简单的HTTP接口，根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置。
# 2. URL:https://restapi.amap.com/v3/ip?parameters
# 3. request.get
# 4. ip

# In[63]:


def ip(ip,sig=None):
    url = "https://restapi.amap.com/v3/ip?parameters"
    ip_params = {
        "key":zhang_key,
        "ip":ip,
        "sig":sig,
        "output":'json'
    }
    response = requests.get(url,params=ip_params)
    results = response.json()
    return results


# In[64]:


results_IP定位=ip('81.71.17.53') 
results_IP定位


# ### 静态地图
# 1. 静态地图服务通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。
# 2. URL:https://restapi.amap.com/v3/staticmap?parameters
# 3. requests.get
# 4. key,location,zoom

# In[60]:


from PIL import Image
from io import BytesIO


# In[61]:


def staticmap(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    url="https://restapi.amap.com/v3/staticmap?parameters"
    params={
        "key":zhang_key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "scale":scale,
        "markers":markers,
        "labels":labels,
        "paths":paths,
        "traffic":traffic,
        "sig":sig,
        "output":'json'
    }
    response = requests.get(url,params=params)
    data=Image.open(BytesIO(response.content))
    return data


# In[62]:


data_静态地图=staticmap(中大南方_location,[1,17],'1024*1024')
data_静态地图


# ### 坐标转换
# 1. 坐标转换是一类简单的HTTP接口，能够将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标。
# 2. URL:https://restapi.amap.com/v3/assistant/coordinate/convert?parameters
# 3. request.get
# 4. locations,coordsys

# In[249]:


def convert(locations,coordsys=None,sig=None):
    url="https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params={
        "key":zhang_key,
        "locations":locations,
        "coordsys":coordsys,
        "sig":sig,
    }
    response=requests.get(url,params=params)
    results=response.json()
    return results


# In[250]:


location_中大南方=113.685752,23.638368


# In[251]:


results_坐标转换=convert(location_中大南方)
results_坐标转换


# ### 天气查询
# 1. 天气查询是一个简单的HTTP接口，根据用户输入的adcode，查询目标区域当前/未来的天气情况。
# 2. url:https://restapi.amap.com/v3/weather/weatherInfo?parameters
# 3. request.get
# 4. city,key

# In[252]:


def weather(city,extensions=None):
    url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params={
        "key":zhang_key,
        "city":city,
        "extensions":extensions
    }
    response=requests.get(url,params=params)
    results=response.json()
    return results


# In[253]:


results_天气查询=weather(440600,'base')
results_天气查询


# ### 输入提示
# 1. 输入提示是一类简单的HTTP接口，提供根据用户输入的关键词查询返回建议列表。
# 2. url:https://restapi.amap.com/v3/assistant/inputtips?parameters
# 3. requests.get
# 4. key,keywords,type,location,city,datatype

# In[254]:


def inputtips(keywords,type=None,location=None,city=None,datatype=all):
    url="https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params={
        "key":zhang_key,
        "keywords":keywords,
        "type":type,
        "location":location,
        "city":city,
        "datatype":datatype
    }
    response=requests.get(url,params=params)
    results=response.json()
    return results


# In[255]:


results_输入提示=inputtips('麦当劳','050302',116.481488,39.990464,'北京')
results_输入提示


# ### 交通态势
# 1. 交通态势是一类简单的HTTP接口，提供根据用户输入的内容能够返回希望查询的交通态势情况。
# * 说明：交通态势接口计划将于2020年12月31日下线，目前不再对新用户开放。如需合作请通过工单进行商务咨询。 *
# 2. url:https://restapi.amap.com/v3/traffic/status/rectangle?parameters
# 3. requests.get
# 4. rectangle,key

# In[258]:


def rectangle(rectangle,level=None,extensions='base'):
    url="https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
    params={
        "key":zhang_key,
        "rectangle":rectangle,
        "level":level,
        "extensions":extensions
    }
    response=requests.get(url,params=params)
    results=response.json()
    return results


# In[277]:


results_交通态势=rectangle(中大南方_location)
results_交通态势


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:




